package com.hyxt.handler;

import com.hyxt.utils.IRedisMessageListener;
import com.hyxt.utils.RedisPoolUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author songm
 * @version v1.0
 * @Description 多点监听redis消息处理类
 * @Date: Create in 17:06 2017/12/19
 * @Modifide By:
 **/
public class MultiListenerHandler extends IRedisMessageListener {

    private static final Log log = LogFactory.getLog(MultiListenerHandler.class);

    //存放各个通道消息的队列
    public static ConcurrentHashMap<String,ArrayBlockingQueue<String>> msgQueue = new ConcurrentHashMap<>();
    /**
     * 处理监听到的消息
     * @param channel
     * @param message
     */
    @Override
    public void onMessage(String channel, String message) {
        if (msgQueue.get(channel) == null) {
            msgQueue.put(channel,new ArrayBlockingQueue<String>(1000));
        }
        Map date = RedisPoolUtil.getDate(message);
        String state = String.valueOf(date.get("state"));
        //如果是删除的消息则不算
        if ("DELETE".equalsIgnoreCase(state)){
        }else {
            try {
                msgQueue.get(channel).put(String.valueOf(date.get("value")));
            } catch (InterruptedException e) {
                e.printStackTrace();
                log.error("监听消息放入队列出错："+e.getMessage());
            }
        }
    }
}
